package com.sqsxblog.hybrid;

import android.os.Bundle;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class WebViewBaseActivity extends AppCompatActivity {

    private WebView mWebView;

    private FrameLayout main;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);

        main = findViewById(R.id.main);

        mWebView = new WebView(getApplicationContext());
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);

        main.addView(mWebView);
    }

    public WebView getWebView() {
        return mWebView;
    }

    @Override
    protected void onDestroy() {
        if (mWebView != null)
        {
            ViewParent parent = mWebView.getParent();
            if (parent != null) {
                ((ViewGroup) parent).removeView(mWebView);
            }
            mWebView.stopLoading();
            mWebView.getSettings().setJavaScriptEnabled(false);
            mWebView.clearHistory();
            mWebView.clearView();
            mWebView.removeAllViews();
            mWebView.destroy();
            mWebView = null;
        }
        super.onDestroy();
    }
}
